﻿using System;
using System.Collections.Generic;
using System.Text;
using TTClassLibrary;
using TTServer.Messages;
using libCommonClass;
using TTServer.Controller;
using libInterface;
using libUtilities;
using System.Drawing;
using bmttPPTController.TTServer;

namespace TTServer.MessageProcessor
{
    public class PPTWindowsRelationshipRequestMessageProcessor : IMessageProcessor
    {
        public bool Process(libCommonClass.TTMessage message, libInterface.IClientInfoProvider clientInfoProvider)
        {
            if (message is PPTWindowsRelationshipRequestMessage)
            {
                PPTWindowsRelationshipRequestMessage msg = (PPTWindowsRelationshipRequestMessage)message;
                
                if (!(clientInfoProvider.Authority == AuthorityConstant.ADMIN
                    || clientInfoProvider.Authority == AuthorityConstant.KEY_MEMBER))
                {
                    return true;
                }

                PPTController pptCtrl = PPTController.Instance;

                if (!pptCtrl.checkPPTApplicationRun())
                {
                    NotifyMessage nm = new NotifyMessage("PPT Controller not run");

                    clientInfoProvider.AddToMessageQueue(nm);

                    return true;
                }

                String[] names = null;
                int activeWindowIndex = -1;

                int re = PPTController.Instance.GetWindowRelationship(ref names, ref activeWindowIndex);

                if (re == 1)
                {
                    PPTWindowsRelationshipResponse wrr = new PPTWindowsRelationshipResponse();
                    wrr.TemporaryKey = clientInfoProvider.TemporaryKey;
                    wrr.PresentationNames = names;
                    wrr.ActiveWindowIndex = activeWindowIndex;

                    clientInfoProvider.AddToMessageQueue(wrr);
                }
                else
                {
                    // Thong bao la khong thuc hien duoc thao tac nay
                    NotifyMessage nm = new NotifyMessage("Fetch data unsuccessfully!!!");

                    clientInfoProvider.AddToMessageQueue(nm);
                }

                return true;
            }

            return false;
        }
    }
}
